import xmlrpc.client as  xmlrpclib
from django.db import models
from django.db.models.signals import post_save
from jsonfield import JSONField


# from supervisor_dj.models_manager import Supervisor_Process_Manager
# from .utils_v3 import SupervisorClass

class SupervisorHostManager(models.Manager):
    def all_processes(self):
        qs = super().all_processes().object.processes.all()
        return qs


class SupervisorHost(models.Model):
    # 连接supervisorHost信息相关
    hostname = models.CharField(verbose_name="主机名", max_length=64)
    ip = models.GenericIPAddressField("ip地址", protocol='IPv4', unique=True)
    port = models.IntegerField(verbose_name="连接端口", default=9001)

    # username = models.CharField("连接supervisor用户名", max_length=32)
    # password = models.CharField("密码", max_length=32)

    # all_processes = SupervisorHostManager()

    def __str__(self):
        return self.hostname

    class Meta:
        index_together = ("hostname", "ip")
        verbose_name = 'supervisor主机'
        verbose_name_plural = verbose_name


def get_supervisor_process_info_to_db_receiver(sender, instance, created, **kwargs):
    if created:
        ip = instance.ip
        port = instance.port


post_save.connect(receiver=get_supervisor_process_info_to_db_receiver, sender=SupervisorHost, )


class Supervisor_Process(models.Model):
    """
    保存进程信息
    """
    name = models.CharField("进程名", max_length=64)
    ip = models.GenericIPAddressField(protocol='IPv4', )
    detail = JSONField(default={}, blank=True, )
    supervisor_host = models.ForeignKey(to=SupervisorHost, on_delete=models.CASCADE, related_name='processes')

    # objects = Supervisor_Process_Manager()

    class Meta:
        unique_together = ("name", "ip")
        verbose_name_plural = '进程信息'
        verbose_name = '进程信息'


class Supervisor_Action_History(models.Model):
    action_name = models.CharField("动作", max_length=16)
    add_time = models.DateTimeField("操作时间", auto_now_add=True)
    ip = models.GenericIPAddressField("ip地址", protocol='IPv4', )
    process_name = models.CharField("进程名", default=None, blank=True, null=True, max_length=64)
    desc = models.CharField("描述", default="0", blank=True, null=True, max_length=512)
    status_code = models.BooleanField("状态", default=True)
    user = models.CharField(max_length=32, default="0", blank=True, null=True, verbose_name="操作用户")

    class Meta:
        ordering = ("-add_time",)
        verbose_name_plural = '操作记录'
        verbose_name = '操作记录'

''' 

# 信号接收
def supervisor_action_historymodel_post_save_receiver(sender, instance, created, *args, **kwargs):
    print("after save")
    print(created)  # True 如果是新建的实例instance, 不是update、delete

    if created:
        instance.save()


# 发出信号
post_save.connect(supervisor_action_historymodel_post_save_receiver, sender=Supervisor_Action_History)
'''

class owgameserver(models.Model):
    serverid = models.IntegerField(u"游戏服ID号", primary_key=True)
    serverhost = models.GenericIPAddressField(u"游戏服IP")
    servername = models.CharField(u"游戏服名称", max_length=32)
    serverport = models.IntegerField(u"游戏服端口号")
